﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;
using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;
using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
    public partial interface IService
    {
        [OperationContract]
        string ProductIN_D_Add(ProductIN_D model, int ACID);

        [OperationContract]
        string ProductIN_D_Update(ProductIN_D model, int ACID);

        [OperationContract]
        string ProductIN_DBydid_Delete(int did, int ACID);

        [OperationContract]
        IList<ProductIN_D> ProductIN_D_Get(string sortBy, int ACID);

    }

    public partial class Service : IService
    {
        #region  添加ProductIN_D表数据
        /// <summary>
        /// 添加ProductIN_D
        /// </summary>
        /// <param name="model">ProductIN_D实体</param>
        /// <returns>空或错误</returns>
        public string ProductIN_D_Add(ProductIN_D model, int ACID)
        {
            if (model.did == null)
            {
                return "『详单流水号』不能为空！";
            }
            if (model.counts == null)
            {
                return "『数量』不能为空！";
            }
            if (string.IsNullOrEmpty(model.unit))
            {
                return "『单位』不能为空！";
            }
            if (model.unitprice == null)
            {
                return "『单价』不能为空！";
            }
            if (string.IsNullOrEmpty(model.billid))
            {
                return "『单据号』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.ProductIN_D.Count(p => p.did == model.did) > 0)
                    {
                        return "主键已存在！";
                    }
                    ctx.ProductIN_D.InsertOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 添加失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  添加ProductIN_D表数据事物用重构
        /// <summary>
        /// 添加ProductIN_D事物用重构
        /// </summary>
        /// <param name="model">ProductIN_D实体</param>
        /// <returns>空或错误</returns>
        private string ProductIN_D_Add(ProductIN_D model, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (model.counts == null)
            {
                return "『数量』不能为空！";
            }
            if (string.IsNullOrEmpty(model.unit))
            {
                return "『单位』不能为空！";
            }
            if (model.unitprice == null)
            {
                return "『单价』不能为空！";
            }
            if (string.IsNullOrEmpty(model.billid))
            {
                return "『单据号』不能为空！";
            }

            ctx.ProductIN_D.InsertOnSubmit(model);
            return string.Empty;
        }
        #endregion

        #region  修改ProductIN_D表数据
        /// <summary>
        /// 修改ProductIN_D
        /// </summary>
        /// <param name="model">ProductIN_D实体</param>
        /// <returns>空或错误</returns>
        public string ProductIN_D_Update(ProductIN_D model, int ACID)
        {
            if (model.did == null)
            {
                return "『详单流水号』不能为空！";
            }
            if (model.counts == null)
            {
                return "『数量』不能为空！";
            }
            if (string.IsNullOrEmpty(model.unit))
            {
                return "『单位』不能为空！";
            }
            if (model.unitprice == null)
            {
                return "『单价』不能为空！";
            }
            if (string.IsNullOrEmpty(model.billid))
            {
                return "『单据号』不能为空！";
            }

            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.ProductIN_D.Count(p => p.did == model.did) != 1)
                    {
                        return "未找到指定记录或记录不唯一！";
                    }
                    ProductIN_D proModel = ctx.ProductIN_D.Single(p => p.did == model.did);
                    if (!string.IsNullOrEmpty(model.storage))
                    {
                        proModel.storage = model.storage;
                    }
                    if (!string.IsNullOrEmpty(model.gcode))
                    {
                        proModel.gcode = model.gcode;
                    }
                    if (!string.IsNullOrEmpty(model.types))
                    {
                        proModel.types = model.types;
                    }
                    if (!string.IsNullOrEmpty(model.unit))
                    {
                        proModel.unit = model.unit;
                    }
                    if (model.counts > 0)
                    {
                        proModel.counts = model.counts;
                    }
                    if (model.disprice > 0)
                    {
                        proModel.disprice = model.disprice;
                    }
                    if (model.discount > 0)
                    {
                        proModel.discount = model.discount;
                    }
                    if (model.unitprice > 0)
                    {
                        proModel.unitprice = model.unitprice;
                    }
                    if (model.amount > 0)
                    {
                        proModel.amount = model.amount;
                    }
                    if (model.taxratio > 0)
                    {
                        proModel.taxratio = model.taxratio;
                    }
                    if (model.tax > 0)
                    {
                        proModel.tax = model.tax;
                    }
                    if (model.amounttax > 0)
                    {
                        proModel.amounttax = model.amounttax;
                    }
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 修改失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  修改ProductIN_D表数据
        /// <summary>
        /// 修改ProductIN_D
        /// </summary>
        /// <param name="model">ProductIN_D实体</param>
        /// <returns>空或错误</returns>
        public string ProductIN_D_Update(ProductIN_D model, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (string.IsNullOrEmpty(model.billid))
            {
                return "『详单流水号』不能为空！";
            }
            if (model.counts == null)
            {
                return "『数量』不能为空！";
            }
            if (string.IsNullOrEmpty(model.unit))
            {
                return "『单位』不能为空！";
            }
            if (model.unitprice == null)
            {
                return "『单价』不能为空！";
            }
            if (string.IsNullOrEmpty(model.billid))
            {
                return "『单据号』不能为空！";
            }

            try
            {
                if (ctx.ProductIN_D.Count(p => p.did == model.did) != 1)
                {
                    return "未找到指定记录或记录不唯一！";
                }
                ProductIN_D proModel = ctx.ProductIN_D.Single(p => p.did == model.did);

                if (!string.IsNullOrEmpty(model.storage))
                {
                    proModel.storage = model.storage;
                }
                if (!string.IsNullOrEmpty(model.gcode))
                {
                    proModel.gcode = model.gcode;
                }
                if (!string.IsNullOrEmpty(model.types))
                {
                    proModel.types = model.types;
                }
                if (!string.IsNullOrEmpty(model.unit))
                {
                    proModel.unit = model.unit;
                }
                if (model.counts > 0)
                {
                    proModel.counts = model.counts;
                }
                if (model.disprice > 0)
                {
                    proModel.disprice = model.disprice;
                }
                if (model.discount > 0)
                {
                    proModel.discount = model.discount;
                }
                if (model.unitprice > 0)
                {
                    proModel.unitprice = model.unitprice;
                }
                if (model.amount > 0)
                {
                    proModel.amount = model.amount;
                }
                if (model.taxratio > 0)
                {
                    proModel.taxratio = model.taxratio;
                }
                if (model.tax > 0)
                {
                    proModel.tax = model.tax;
                }
                if (model.amounttax > 0)
                {
                    proModel.amounttax = model.amounttax;
                }

                onhand h = GetonhandByMoreKey(model.gcode, model.storage, ACID);
                if (h != null)
                {
                    h.onhandcount -= decimal.Parse(proModel.counts.ToString());
                    onhand_Update(h, ACID, ctx);
                }

                //ctx.SubmitChanges();
                return string.Empty;
            }
            catch (Exception ex)
            {
                return " 修改失败!错误详细信息：" + ex.Message;
            }
        }
        #endregion

        /// <summary>
        /// 获取ProductIN_D
        /// </summary>
        /// <param name="predicate"></param>
        /// <param name="sortBy"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public IList<ProductIN_D> ProductIN_D_Get(string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    return ProductIN_D_Get(null, sortBy, ACID);
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }

        #region  获取ProductIN_D表数据 
        /// <summary>
        /// 获取ProductIN_D
        /// </summary>
        /// <param name="predicate">排序属性名</param>
        /// <param name="sortBy">排序类型0=升序 1=降序</param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public List<ProductIN_D> ProductIN_D_Get(Expression<Func<ProductIN_D, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.ProductIN_D.ToList();
                        }
                        else
                        {
                            return ctx.ProductIN_D.OrderBy(sortBy).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.ProductIN_D.Where(predicate).ToList();
                        }
                        else
                        {
                            return ctx.ProductIN_D.Where(predicate).OrderBy(predicate).ToList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion

        #region  删除ProductIN_D表Bydid数据
        /// <summary>
        /// 删除CardIN_DBydid
        /// </summary>
        /// <param name="did">详单流水号</param>
        /// <returns>空或错误</returns>
        public string ProductIN_DBydid_Delete(int did, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.ProductIN_D.Count(p => p.did == did) != 1)
                    {
                        return null;
                    }
                    ProductIN_D model = ctx.ProductIN_D.Single(p => p.did == did);
                    ctx.ProductIN_D.DeleteOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 删除失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  删除ProductIN_D表Bydid数据[事物用重构]
        /// <summary>
        /// 删除CardIN_DBydid[事物用重构]
        /// </summary>
        /// <param name="did">详单流水号</param>
        /// <returns>空或错误</returns>
        private string ProductIN_DBydid_Delete(int did, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (did <= 0)
            {
                return "『详单流水号』不能为空！";
            }
            if (ctx.ProductIN_D.Count(p => p.did == did) != 1)
            {
                return null;
            }
            ProductIN_D model = ctx.ProductIN_D.Single(p => p.did == did);
            ctx.ProductIN_D.DeleteOnSubmit(model);
            return string.Empty;
        }
        #endregion
    }
}
